package map.android.baidu.carpool.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import map.android.baidu.carpool.R;
import map.android.baidu.carpool.model.CarPoolType;
import map.android.baidu.carpool.response.MainConfigResponse;

/**
 * Created by zhaoyangyang on 2015/3/27.
 */
public class CarpoolViewPagerAdapter extends PagerAdapter {
    // 返回ViewPager包含的不觉的个数
    private List<View> list = null;
    private Context mContext = null;
    private ArrayList<CarPoolType> carTypeList = null;
    private boolean hasPrice = false;
    /*private ArrayList<ArrayList<MainConfigResponse.MainConfig.CarTypeItem>> carTempList =
            new ArrayList<ArrayList<MainConfigResponse.MainConfig.CarTypeItem>>();*/
    private ArrayList<CarPoolType> carOnclickList = new ArrayList<CarPoolType>();
    private ArrayList<CarPoolType> carOnclickListClone = null;

    public CarpoolViewPagerAdapter(Context mContext, List<View> list, ArrayList<CarPoolType> carTypeList,
                                   boolean hasPrice) {
        this.list = list;
        this.mContext = mContext;
        this.carTypeList = carTypeList;
        this.hasPrice = hasPrice;
        carOnclickList.clear();
        carOnclickList.addAll(this.carTypeList);
        carOnclickListClone = (ArrayList<CarPoolType>)carOnclickList.clone();
        //initTempList();
    }

    private void initTempList() {
       /* carTempList.clear();
        int k = 0;
        for (int i = 1; i <= carTypeList.size(); i++) {
            k = k + 1;
            ArrayList<MainConfigResponse.MainConfig.CarTypeItem> carTempList = new ArrayList<MainConfigResponse
                    .MainConfig.CarTypeItem>();
            carTempList.add(carTypeList.get(i));

            if (k == 3){
                carTypeList.remove(carTypeList.get(0));
            }

        }*/

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    // 绑定一个特殊的对象 这个对象由instantiateItem返回
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    // 销毁View
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView(list.get(position));
    }

    // 初始化
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        // TODO Auto-generated method stub
        View view = list.get(position);

        LinearLayout lin1 = (LinearLayout) view.findViewById(R.id.lin_plug_car_models).findViewById(R.id
                .layout_car_type_1);
        LinearLayout lin2 = (LinearLayout) view.findViewById(R.id.lin_plug_car_models).findViewById(R.id
                .layout_car_type_2);
        LinearLayout lin3 = (LinearLayout) view.findViewById(R.id.lin_plug_car_models).findViewById(R.id
                .layout_car_type_3);
        final ImageView oneIV = (ImageView) lin1.findViewById(R.id.iv_choose_type);
        final ImageView TwoIV = (ImageView) lin2.findViewById(R.id.iv_choose_type);
        final ImageView ThreeIV = (ImageView) lin3.findViewById(R.id.iv_choose_type);

        TextView oneCarType = (TextView) lin1.findViewById(R.id.tv_price_type);
        TextView twoCarType = (TextView) lin2.findViewById(R.id.tv_price_type);
        TextView ThreeCarType = (TextView) lin3.findViewById(R.id.tv_price_type);

        TextView oneCarName = (TextView) lin1.findViewById(R.id.tv_car_type);
        TextView TwoCarName = (TextView) lin2.findViewById(R.id.tv_car_type);
        TextView ThreeCarName = (TextView) lin3.findViewById(R.id.tv_car_type);

        TextView onePrice = (TextView) view.findViewById(R.id.tv_price_1);
        TextView TwoPrice = (TextView) view.findViewById(R.id.tv_price_2);
        TextView ThreePrice = (TextView) view.findViewById(R.id.tv_price_3);

        onePrice.setVisibility(View.GONE);
        TwoPrice.setVisibility(View.GONE);
        ThreePrice.setVisibility(View.GONE);

        ViewOnClick oneOnClick = new ViewOnClick();
        ViewOnClick twoOnClick = new ViewOnClick();
        ViewOnClick threeOnClick = new ViewOnClick();

        oneIV.setTag(oneOnClick);
        TwoIV.setTag(twoOnClick);
        ThreeIV.setTag(threeOnClick);

        lin1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (position * 3) + 0;
                if (((ViewOnClick) oneIV.getTag()).isChoose) {
                    oneIV.setImageResource(R.drawable.home_weixuanzhong);
                    carOnclickList.remove(carOnclickListClone.get(pos));
                    ((ViewOnClick) oneIV.getTag()).isChoose = false;
                } else {
                    oneIV.setImageResource(R.drawable.home_xuanzhong);
                    carOnclickList.add(carOnclickListClone.get(pos));
                    ((ViewOnClick) oneIV.getTag()).isChoose = true;
                }
                // notifyDataSetChanged();
            }
        });

        lin2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (position * 3) + 1;

                if (((ViewOnClick) TwoIV.getTag()).isChoose) {
                    TwoIV.setImageResource(R.drawable.home_weixuanzhong);
                    carOnclickList.remove(carOnclickListClone.get(pos));
                    ((ViewOnClick) TwoIV.getTag()).isChoose = false;
                } else {
                    TwoIV.setImageResource(R.drawable.home_xuanzhong);
                    carOnclickList.add(carOnclickListClone.get(pos));
                    ((ViewOnClick) TwoIV.getTag()).isChoose = true;
                }
            }
        });

        lin3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (position * 3) + 2;
                if (((ViewOnClick) ThreeIV.getTag()).isChoose) {
                    ThreeIV.setImageResource(R.drawable.home_weixuanzhong);
                    carOnclickList.remove(carOnclickListClone.get(pos));
                    ((ViewOnClick) ThreeIV.getTag()).isChoose = false;
                } else {
                    ThreeIV.setImageResource(R.drawable.home_xuanzhong);
                    carOnclickList.add(carOnclickListClone.get(pos));
                    ((ViewOnClick) ThreeIV.getTag()).isChoose = true;
                }
            }
        });

        for (int i = 0; i < carTypeList.size(); i++) {
            if (i == 0) {
                oneCarType.setText(carTypeList.get(0).name);
                oneCarName.setText(carTypeList.get(0).desc);
                if (hasPrice) {
                    onePrice.setVisibility(View.VISIBLE);
                    onePrice.setText("¥ " + carTypeList.get(0).fee);
                } else {
                    onePrice.setVisibility(View.GONE);
                }
                oneIV.setImageResource(R.drawable.home_xuanzhong);
                //  isOnclickLin1 = true;
            } else if (i == 1) {
                twoCarType.setText(carTypeList.get(1).name);
                TwoCarName.setText(carTypeList.get(1).desc);
                if (hasPrice) {
                    TwoPrice.setVisibility(View.VISIBLE);
                    TwoPrice.setText("¥ " + carTypeList.get(1).fee);
                } else {
                    TwoPrice.setVisibility(View.GONE);
                }
                TwoIV.setImageResource(R.drawable.home_xuanzhong);
                // isOnclickLin2 = true;
            } else {
                ThreeCarType.setText(carTypeList.get(2).name);
                ThreeCarName.setText(carTypeList.get(2).desc);
                if (hasPrice) {
                    ThreePrice.setVisibility(View.VISIBLE);
                    ThreePrice.setText("¥ " + carTypeList.get(2).fee);
                } else {
                    ThreePrice.setVisibility(View.GONE);
                }
            }
            ThreeIV.setImageResource(R.drawable.home_xuanzhong);
            //  isOnclickLin3 = true;
        }

        ((ViewPager) container).addView(list.get(position));
        return list.get(position);
    }

    public class ViewOnClick {
        public boolean isChoose = true;
    }

}


